/***********************************************************************************************************************
* Copyright [2015-2017] Renesas Electronics Corporation and/or its licensors. All Rights Reserved.
*
* The contents of this file (the "contents") are proprietary and confidential to Renesas Electronics Corporation
* and/or its licensors ("Renesas") and subject to statutory and contractual protections.
*
* Unless otherwise expressly agreed in writing between Renesas and you: 1) you may not use, copy, modify, distribute,
* display, or perform the contents; 2) you may not use any name or mark of Renesas for advertising or publicity
* purposes or in connection with your use of the contents; 3) RENESAS MAKES NO WARRANTY OR REPRESENTATIONS ABOUT THE
* SUITABILITY OF THE CONTENTS FOR ANY PURPOSE; THE CONTENTS ARE PROVIDED "AS IS" WITHOUT ANY EXPRESS OR IMPLIED
* WARRANTY, INCLUDING THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
* NON-INFRINGEMENT; AND 4) RENESAS SHALL NOT BE LIABLE FOR ANY DIRECT, INDIRECT, SPECIAL, OR CONSEQUENTIAL DAMAGES,
* INCLUDING DAMAGES RESULTING FROM LOSS OF USE, DATA, OR PROJECTS, WHETHER IN AN ACTION OF CONTRACT OR TORT, ARISING
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THE CONTENTS. Third-party contents included in this file may
* be subject to different terms.
* Author: K2 Power Inc.
**********************************************************************************************************************/


#ifndef DATA_FLASH_HELPER_H
#define DATA_FLASH_HELPER_H

#define DATA_FLASH_BLOCK_SIZE           (64) //bytes

#define INTERNAL_DF_BASE_ADDR           (0x08000000)

#define LOG_METADATA_BLOCK_ADDR         (INTERNAL_DF_BASE_ADDR)
#define LOG_BLOCK_LEN                   (DATA_FLASH_BLOCK_SIZE)                              ///< 0x40100000-0x40100040, 64 bytes

#define NUM_OF_BLOCKS(x)                ((x) / DATA_FLASH_BLOCK_SIZE)

/*************************/
/* Flash operations event ids */
#define ERASE_COMPLETE_EVT              0x00000001
#define WRITE_COMPLETE_EVT              0x00000002
#define AREA_BLANK_EVT                  0x00000004
#define AREA_NOT_BLANK_EVT              0x00000008
#define ERR_FAILURE_EVT                 0x00000010

/* Info type IDs */
typedef enum e_int_storage_type {
    PERSISTENT_LOG_METADATA_TYPE
} int_storage_type_t;

typedef struct session_header
{
    uint8_t                 is_fw_loaded;                          //1
    uint32_t                last_firmware_update_timestamp;        //4
    uint32_t                fw_image_size;                         //4
    uint32_t                valid;                                 //4
    uint8_t                 unique_id[32];                         //32 = 45 bytes
} session_header_t;

typedef struct
{
    session_header_t        session;    //45
    uint32_t                crc_value;  //4 = 49 bytes
}session_header_crc_t;

/****************************************** Internal Storage Helper Functions ************************************************/
/** Initialize data flash.
     * @pre None
     * @param None
     * @return 0 on successful initialization
 */
extern fsp_err_t internal_storage_init(void);

/** Read from data flash
     * @pre None
     * @param[out] data_buffer Buffer into which data will be read
     * @param[in]  data_size Size of buffer
     * @param[in]  info_type Type of data to be read
     * @return 0 on succes, non-zero on failure
 */
extern fsp_err_t internal_storage_read(uint8_t *data_buffer, unsigned int data_size, unsigned int info_type);

/** Write to data flash
     * @pre None
     * @param[in] data_buffer Buffer containing data to be written
     * @param[in] data_size Size of data to be written
     * @param[in] info_type Type of data to be written
     * @return 0 on success, non-zero on failure
 */
extern fsp_err_t internal_storage_write(uint8_t *data_buffer, unsigned int data_size, unsigned int info_type);

/** Erase Data flash location
     * @pre None
     * @param[in] info_type Type of data to be written
     * @return 0 on success, non-zero on failure
*/
extern fsp_err_t internal_storage_erase(int_storage_type_t info_type);

/** Close data flash driver
     * @pre None
     * @param None
     * @return 0 on success, non-zero on error
 */
extern fsp_err_t internal_storage_deinit(void);

#endif /* DATA_FLASH_HELPER_H */
